#include "edudialog.h"
#include "ui_edudialog.h"
#include<QString>
#include<QDebug>
#include<QMessageBox>
#include<QSqlDatabase>
#include<QtSql>
#include"qualifydocu.h"
#include"sqlconnect.h"
eduDialog::eduDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::eduDialog)
{
    ui->setupUi(this);
    //学历
    QStringList strings;
    QSqlQuery query("SELECT Education FROM EducationCode ");
    while (query.next()) {
    QString edu = query.value(0).toString();
    strings.append(edu);
    }
    completer = new QCompleter(strings, this);
    ui->eduNameBox->clear();
    ui->eduNameBox->addItems(strings);
    ui->eduNameBox->setEditable(true);
    ui->eduNameBox->setCompleter(completer);
    //学位
    QStringList strings3;
    QSqlQuery query3("SELECT Degree FROM DegreeCode");
    while (query3.next()) {
    QString degree = query3.value(0).toString();
    strings3.append(degree);
    }
    completer = new QCompleter(strings3, this);
    ui->academicBox->clear();
    ui->academicBox->addItems(strings3);
    ui->academicBox->setEditable(true);
    ui->academicBox->setCompleter(completer);
    connect(ui->cancel,SIGNAL(clicked(bool)),this,SLOT(close()));
    connect(ui->confirm,SIGNAL(clicked(bool)),this,SLOT(react()));
    connect(ui->confirm,SIGNAL(clicked(bool)),parent,SLOT(showEduInfo()));
}

eduDialog::~eduDialog()
{
    delete ui;
}
void eduDialog::react(){
    QString start=ui->startdate->text();
    QString end=ui->enddate->text();
    //QDate start=ui->startdate->date();
    //QDate end=ui->enddate->date();
    QString academic=ui->academicBox->currentText();
    QSqlQuery q1("select DegreeID from DegreeCode where Degree='"+academic+"'");
    while(q1.next()){
        academic=q1.value(0).toString();
    }
    QString edu=ui->eduNameBox->currentText();
    QSqlQuery q2("select EducationID from EducationCode where Education='"+edu+"'");
    while(q2.next()){
        edu=q2.value(0).toString();
    }
    QString eduState=ui->resultBox->currentText();//学历完成情况
    QString allDay=ui->allDayBox->currentText();
    if(allDay=="在职")
        allDay="0";
    if(allDay=="全日制")
        allDay="1";
    QString school=ui->schoolEdit->text();
    QString major=ui->majorEdit->text();
    if(school.count()==0||major.count()==0){
        QMessageBox::information(this,"提示信息","学校或专业不得为空！");
        return;
        }
    //QString state;
   if(eduState==QString::fromLocal8Bit("毕业")){
        eduState="1";
    }
    if(eduState==QString::fromLocal8Bit("结业")){
        eduState="2";
    }
    if(eduState==QString::fromLocal8Bit("未结业")){
        eduState="3";
    }
    if(eduState==QString::fromLocal8Bit("肄业")){
        eduState="4";
    }
    if(eduState==QString::fromLocal8Bit("其他")){
        eduState="9";
    }
    //qDebug()<<start<<" "<<end;
    //sqlconnect(db);
    QSqlQuery qu("select * FROM `Edu` WHERE `StartTime`='"+start+"'and School='"+school+"' and `Major`='"+major+"' and IDNum='"+ID+"'");
    if(qu.next())
    {
        QMessageBox::information(this,"提示信息","该条教育经历已存在，不可重复添加！");
        return;
    }




    QSqlQuery query;
    QString sql="INSERT INTO `Edu`(StartTime,EndTime,EduStateID,EduName,AcademicDe,School,Major,WorkORSchool,IDNum)VALUES('"+start+"','"+end+"','"+eduState+"','"+edu+"','"+academic+"','"+school+"','"+major+"','"+allDay+"','"+ID+"')";
    bool ok=query.exec(sql);
    //qDebug()<<ok;
    if(ok){
        QMessageBox::information(this,"提示信息","学历成功插入!");
    }

    //db.close();
    this->close();
}

void eduDialog::reciveID(QString s){
    ID=s;
}
